package com.factory._13observe.demo2;

import com.factory._13observe.demo1.Observer;

import java.util.Enumeration;
import java.util.Vector;

/**
 * @author Qinye
 * @date 2023/8/13 16:21
 */
public abstract class Subject {
    private Vector vector = new Vector();

    public  void  attach(Observer observer){
        vector.addElement(observer);
        System.out.println("attach an observer");
    }

    public void detach(Observer observer) {
        vector.removeElement(observer);
    }

    public  void  notifyObservers(){
        Enumeration enumeration = observers();
        while (enumeration.hasMoreElements()){
            ((Observer)enumeration.nextElement()).update();
        }
    }

    private Enumeration observers() {
        return((Vector) vector.clone()).elements();
    }
}
